package com.fast.fastenjoy.base.utils;

import java.lang.reflect.Field;
import java.lang.reflect.Method;

import org.apache.commons.beanutils.BeanUtils;
import org.apache.commons.lang.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

/**
 * 处理Bean工具类
 * @author chenliaohua
 *
 */
public class BeanExUtils extends BeanUtils{

	private static final Logger LOG = LoggerFactory.getLogger(BeanExUtils.class);
	
	private static final String GET = "get";
	
	private static final String IS = "is";
	
	public static Object getPropertyValue(Object bean, String propertyName, Object...objects) throws Throwable{
		
		Method method = getMethod(bean.getClass(), propertyName);
		
		if(method == null){
			return null;
		}
		
		return method.invoke(bean, objects);
	}
	
	/**
	 * 获取getXXX方法
	 * @param bean
	 * @param propertyName
	 * @return
	 */
	public static Method getMethod(Class<? extends Object> clazz, String propertyName) {
		
		if(clazz == null || StringUtils.isBlank(propertyName)){
			return null;
		}
		
		StringBuilder _get = new StringBuilder(GET).append(Character.toUpperCase(propertyName.charAt(0)));
		StringBuilder _is = new StringBuilder(IS).append(Character.toUpperCase(propertyName.charAt(0)));
		
		if (propertyName.length() > 1) {
			_get.append(propertyName.substring(1));
			_is.append(propertyName.substring(1));
		}
		
		Method method = null;
		
		try {
			method = clazz.getMethod(_get.toString());
		} catch (Exception e) {
			if(method == null){
				try {
					method = clazz.getMethod(_is.toString());
				} catch (Exception e1) {
				} 
			}
		} 
		
		return method;
	}

	/**
	 * 获取属性
	 * @param bean
	 * @param propertyName
	 * @return
	 */
	public static Field getField(Object bean, String propertyName) {
		for (Field f : bean.getClass().getDeclaredFields()) {
			if (propertyName.equals(f.getName())) {
				return f;
			}
		}
		return null;
	}

	/**
	 * 获取Long类型属性数据
	 * @param bean
	 * @param propertyName
	 * @return
	 * @throws NoSuchFieldException
	 */
	public static long getLongBeanProperty(Object bean, String propertyName)
			throws NoSuchFieldException {
		
		validateArgs(bean, propertyName);

		Object o = getBeanProperty(bean, propertyName);

		if (o == null)
			throw new NoSuchFieldException(propertyName);
		if (!(o instanceof Number)) {
			throw new IllegalArgumentException(propertyName + " not an Number");
		}
		
		return ((Number) o).longValue();
	}

	/**
	 * 判断是否存在该数据
	 * @param bean
	 * @param propertyName
	 * @return
	 */
	public static boolean hasProperty(Object bean, String propertyName){
		
		try{
			
			validateArgs(bean, propertyName);
			
			Field field = getField(bean, propertyName);
			
			if(field != null){
				return true;
			}
			
			Method getter = getMethod(bean.getClass(), propertyName);
			if(getter != null){
				return true;
			}
			
		}catch(Exception e){
		}
		
		return false;
	}
	
	/**
	 * 验证参数
	 * 
	 * @param bean
	 * @param propertyName
	 */
	public static void validateArgs(Object bean, String propertyName) throws IllegalArgumentException{
		if (bean == null) {
			throw new IllegalArgumentException("bean is null");
		}
		if (propertyName == null) {
			throw new IllegalArgumentException("propertyName is null");
		}
		if (propertyName.trim().length() == 0) {
			throw new IllegalArgumentException("propertyName is empty");
		}
	}
	
	/**
	 * 获取String属性数据
	 * @param bean
	 * @param fieldName
	 * @return
	 */
	public static String getStringProperty(Object bean, String fieldName) {

		String result = String.valueOf(getBeanProperty(bean, fieldName));

		return result;
	}

	/**
	 * 获取bean对应的属性
	 * @param bean
	 * @param propertyName
	 * @return
	 */
	public static Object getBeanProperty(Object bean, String propertyName) {
		
		Object obj = null;

		try{
			
			validateArgs(bean, propertyName);
			
			Method getter = getMethod(bean.getClass(), propertyName);

			if (getter != null) {
				try {
					obj = getter.invoke(bean, new Object[0]);
				} catch (Exception ex) {
					Field field = getField(bean, propertyName);
					if (field != null){
						try {
							field.setAccessible(true);
							obj = field.get(bean);
						} catch (Exception e) {
							throw e;
						}
					}
				}
			}
			
		}catch(Exception e){
			LOG.error("BeanExUtils.getBeanProperty error, message:" + e.getMessage());
		}

		return obj;
	}

}
